﻿using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSDLib.Models
{
    public class DownloadTask
    {
        public dl_task Task { get; set; }
        public List<dl_task_channel> ChannelList { get; set; }

        private int CurrentDownloadChannel = 0;

        public DownloadTask()
        {
            ChannelList = new List<dl_task_channel>();
        }

        public dl_task_channel GetCurrentChannel()
        {
            if (ChannelList != null && ChannelList.Count > 0 && CurrentDownloadChannel < ChannelList.Count)
            {
                return ChannelList[CurrentDownloadChannel];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 将当前任务的下载序列移动到下一个可用的下载通道上
        /// </summary>
        public void MoveToNextChannel()
        {
            if (ChannelList.Count > 0)
            {
                if (CurrentDownloadChannel == ChannelList.Count - 1)
                {
                    CurrentDownloadChannel = 0;
                }
                else
                {
                    if (CurrentDownloadChannel + 1 < ChannelList.Count)
                    {
                        CurrentDownloadChannel += 1;
                    }
                }
            }
            else
            {
                CurrentDownloadChannel = 0;
            }
        }
    }

    public class DownloadTaskList : List<DownloadTask>
    {
        private int CurrentDownloadTask { get; set; }

        public void Reset()
        {
            CurrentDownloadTask = 0;
        }

        public DownloadTask GetCurrentDownloadTask()
        {
            if (this.Count > 0 && CurrentDownloadTask < this.Count)
            {
                return this[CurrentDownloadTask];
            }
            else
            {
                return null;
            }
        }

        public void MoveToNextTask()
        {
            if (this.Count > 0)
            {
                if (CurrentDownloadTask == this.Count - 1)
                {
                    CurrentDownloadTask = 0;
                }
                else
                {
                    if (CurrentDownloadTask + 1 < this.Count)
                    {
                        CurrentDownloadTask += 1;
                    }
                }
            }
            else
            {
                CurrentDownloadTask = 0;
            }
        }
    }
}
